package org.lzfto.flink.demo.utils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import org.lzfto.flink.demo.common.crypto.AESCryptoUtil;
import org.lzfto.flink.demo.system.config.Account;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

import com.alibaba.fastjson2.JSON;

public class SecurityUtil {
	
	/**
     * 从account.txt读取加密内容并解密为Account对象
     * @param password 密码
     * @return 解密后的Account对象
     * @throws IOException 读取文件失败时抛出
     */
    public static Account getAccount(String password) throws IOException {
        // 读取加密内容
        ClassPathResource resource = new ClassPathResource("account.txt");
        String encrypted = FileCopyUtils.copyToString(
            new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8));
        
        // 解密        
        AESCryptoUtil crypto = new AESCryptoUtil(password);
        String json = crypto.decrypt(encrypted.trim());
        
        // 反序列化为Account对象
        return JSON.parseObject(json, Account.class);
    }

}
